﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using VietPay.KooBoo.Data.Infrastructure;
using VietPay.KooBoo.Business.Models;
using VietPay.KooBoo.Business.Interfaces;
using VietPay.KooBoo.Data.Interfaces;
using VietPay.KooBoo.Data;
using VietPay.KooBoo.Data.EntityModels;

namespace VietPay.KooBoo.Business
{
    public class CategoryService : ServiceBase<ICategoryModel>, ICategoryService
    {
        public CategoryService()
            : this(new CategoryRepository())
        {
        }

        private ICategoryRepository _newsRepository;
        public CategoryService(ICategoryRepository newsRepository)
        {
            _newsRepository = newsRepository ?? new CategoryRepository();
        }

        protected override Type LogPrefix
        {
            get { return this.GetType(); }
        }

        public CategoryListViewModel GetCategoryList(int PageSize, int CurrentPage)
        {
            try
            {
                if ((CurrentPage == 0) || (PageSize == 0))
                    return null;

                IQueryable<Category> query = _newsRepository.GetQueryable();
                CategoryListViewModel model = new CategoryListViewModel();
                model.CategoryList= new List<CategoryViewModel>();
                AutoMapper.Mapper.CreateMap<Category, CategoryViewModel>()
                    .ForMember(dest => dest.CategoryID, opt => opt.MapFrom(src => src.CategoryID))
                    .ForMember(dest => dest.CategoryName, opt => opt.MapFrom(src => src.CategoryName));
                model.CategoryList = AutoMapper.Mapper.Map(query.ToList(), model.CategoryList);

                return model;
            }
            catch (System.Exception e)
            {
                this.LogError("Error getting the document list", e);
                return null;
            }
        }


        public CategoryViewModel GetSingle(Expression<Func<CategoryViewModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public void Add(CategoryViewModel entity)
        {
            Category news=new Category();

            news.CategoryName=entity.CategoryName;
            _newsRepository.Add(news);
        }

        public void Delete(CategoryViewModel entity)
        {
            Category news = _newsRepository.GetAll(p => p.CategoryID == entity.CategoryID).FirstOrDefault();
            _newsRepository.Delete(news);
        }

        public void Update(CategoryViewModel entity)
        {           
            Category news =_newsRepository.GetAll(p => p.CategoryID == entity.CategoryID).FirstOrDefault();
            news.CategoryName= entity.CategoryName;
            _newsRepository.Attach(news);
        }

        public IList<CategoryViewModel> GetAll(Expression<Func<CategoryViewModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public IList<CategoryViewModel> GetAll()
        {
            throw new NotImplementedException();
        }

        public IQueryable<CategoryViewModel> Query(Expression<Func<CategoryViewModel, bool>> whereCondition)
        {
            List<CategoryViewModel> newsQuery = new List<CategoryViewModel>();
            IQueryable<Category> query = _newsRepository.GetQueryable();
            AutoMapper.Mapper.CreateMap<Category, CategoryViewModel>()
                .ForMember(dest => dest.CategoryID, opt => opt.MapFrom(src => src.CategoryID))
                .ForMember(dest => dest.CategoryName, opt => opt.MapFrom(src => src.CategoryName));
            newsQuery = AutoMapper.Mapper.Map(query, newsQuery);
            return newsQuery.AsQueryable().Where(whereCondition);
        }

        public long Count(Expression<Func<CategoryViewModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public long Count()
        {
            throw new NotImplementedException();
        }
    }
}
